﻿using System.Collections;
using System.Collections.Generic;
using Gp.Scripts.Equip;
using Gp.Scripts.Utils;

namespace Gp.Scripts.Core {
    public class WeaponAttackEntity : SkillEntity, IArmorBreakSkill {
        // public WeaponAttackEntity(SkillEntityConfig config) : base(config) { }
        private UnitEquipCom ComEquip => UnitReference.ComEquip;
        public WeaponEntity CurWeapon => ComEquip.CurWeapon;


        /// <summary>
        /// 使下一次攻击无消耗的标志位。会在下一次攻击后清空标志位
        /// true : 攻击无消耗
        /// </summary>
        public bool FreeAttack { get; set; } = false;


        public override CostWrapper SkillCostInfo()
        {
            var cost = UnitReference.ComEquip.CurWeapon.Config.ActionCost;
            // 每一点技巧增加 5% 攻速
            var speed = UnitReference.ComStat[StatField.Technic] * 0.05f + 1;

            // 根据武器计算消耗
            return CostWrapper.Any(new ResourceCost(CostField.ActionTime, (cost / speed).Round()));
        }

        // public CostWrapper AttackCostInfo() {
        //     return SkillCostInfo();
        // }


        public override IEnumerator Act(SkillContext context) {
            // ============================ 预攻击处理 ================================
            // =========================== 单位攻击开始 ================================
            // if (context is not UnitSelectContext {Unit: {} u}) yield break;
            yield return ExecuteWeaponAttack(new AttackContext(context.TargetUnit));
            
            // =========================== 攻击后处理 ================================
            // FreeAttack = false;
        }


        /// 调用使单位发动攻击
        public IEnumerator ExecuteWeaponAttack(AttackContext args) {
            // if (args.TargetCell == null) yield break;
            yield return WeaponAttackProcess(args);
        }        
        
        
        // public IEnumerator ExecuteWeaponAttack(AttackContext args, Action<AttackActionEntity> preProcess, bool calReaction = true) {
        //     yield return ExecuteWeaponAttack(args, ModifierValue.Zero, calReaction);
        // }
        
        
        /// 调用使单位发动攻击
        // public IEnumerator ExecuteWeaponAttack(AttackContext args, ModifierValue bonus, bool calReaction = true) {
        //     if (args.TargetCell == null) yield break;
        //
        //     IsPlaying = true;
        //     yield return WeaponAttackProcess(args, calReaction, e => e.BonusDamage(bonus));
        //     UnitReference.Model.PlayAfterAttack();
        //     IsPlaying = false;
        // }
        

        private IEnumerator WeaponAttackProcess(AttackContext args) {
            // WeaponEntity weaponEntity = CurWeapon;
            var callReaction = args.CallReaction;
            var preProcess = args.PreProcess;
            
            
            // 直接发起攻击
            int freq = 1; 
                // args.UseAccelerate ? Helpers.BattleLogic.ResolveAttackFrequency(weaponEntity, UnitReference) : 1;
            
            var attackTarget = args.Target;
            if (attackTarget == null) yield break;
            
            
            for (int i = 0; i < freq; i++) {
                // 防御点 + 1 
                UnitReference.ComCost.AddResource(CostField.Reaction, 1);
                yield return BattleLogic.PlayAttack(UnitReference, attackTarget, this.AtkAction(attackTarget, null));
                yield break;
            }
            
            

        }

        // /// <summary>
        // /// 判断在触发反应后是否仍满足攻击条件
        // /// </summary>
        // /// <param name="args"></param>
        // /// <returns></returns>
        // private bool ContinueAttack(AttackPreReaction args) {
        //     return !Global.Get<BattleSystem>().IsUnitDestroyed(UnitReference)
        //         && UnitReference.ComEquip.CanAttack();
        // }


        public override List<VBattleCell> CalSkillRange()
        {
            var weaponRange = CurWeapon.Config.AtkRange;
            return Helpers.BattleHelper.GetRangeByRadius(UnitReference, weaponRange);
        }

        //
        // public override AttackActionResult PreCalculateResult(BaseUnit unit) {
        //     return this.AutoCalResult(unit);
        // }


        // public override void OnUnitClick(BaseUnit unit) {
        //     if (!this.CanSelect()) return;
        //     HumanExecute(new SkillContext(){TargetUnit = unit});
        // }


        // /// <summary>
        // /// 判断该单位能否施放攻击技能
        // /// </summary>
        // /// <returns></returns>
        // public override DisableReason CanPerform() {
        //     return base.CanPerform()
        //         .And(DS_Reason.None, () =>
        //             UnitReference.ComEquip.CanAttack()
        //             && Global.Get<BattleSystem>().CurTurnState == TurnState.Attack);
        // }
        

        public int BreakCount() {
            // var value = CurWeapon.Info is MeleeWeaponInfo info ? info.BreakThroughCount() : 0;
            return 1;
        }
    }
}